//
//  NSString+Replace.m
//  高仿暴走斗图
//
//  Created by key on 16/3/18.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "NSString+Replace.h"

@implementation NSString (Replace)


#pragma mark - Replace Special Characters

+ (NSString *)replaceBlankCharacterOfString:(NSString *)string {
 
    return [NSString replaceSpecialCharactersOfString:string withSpecialSymbol:@""];
}

+ (NSString *)replaceSpecialCharactersOfString:(NSString *)string withSpecialSymbol:(NSString *)symbol {
    
    return [NSString replaceSpecialCharactersOfString:string withSpecialSymbol:symbol newSymbol:@""];
}

+ (NSString *)replaceSpecialCharactersOfString:(NSString *)string withSpecialSymbol:(NSString *)specialSymbol newSymbol:(NSString *)newSymbol {
    
    return [string stringByReplacingOccurrencesOfString:specialSymbol withString:newSymbol];
}



#pragma mark - Insert String

- (NSString *)insertStringOfContent:(NSString *)content withIndex:(NSInteger)index {
    
    NSMutableString *newString = [NSMutableString stringWithString:self];
    [newString insertString:content atIndex:index];
    
    return newString;
}


#pragma mark - Replace Special String

+ (NSString *)replaceRangeOfString:(NSString *)string regretNothingResults:(NSArray *)results placeholder:(NSString *)placeholder {
    
    if (string.length == 0 || placeholder.length == 0) {
        return nil;
    }
    NSString *newString         = string;
    NSArray *newResults         = results;
    NSString *newPlaceholder    = placeholder;
    
    NSMutableArray *contents = [NSString getRangeStr:newString findText:newPlaceholder];               //获取要重组的字符串中所有特殊占位符的location
    if (!contents) {
        return nil;
    }
    
    NSRange range = {0, 0};
    
    for (NSInteger i = 0; i < newResults.count; i ++) {
        
        range = NSMakeRange([[contents firstObject] integerValue], placeholder.length);                //通过特殊符号的location得到Range
        newString = [newString stringByReplacingCharactersInRange:range withString:newResults[i]];     //把新内容替换掉占位符
        contents = [NSString getRangeStr:newString findText:newPlaceholder];                           //获取新的字符串的占位符的location集合
    }
    
    return newString;
}

+ (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText {
    
    NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:20];
    if (findText == nil && [findText isEqualToString:@""]) {
        return nil;
    }
    NSRange rang = [text rangeOfString:findText];
    if (rang.location != NSNotFound && rang.length != 0) {
        [arrayRanges addObject:[NSNumber numberWithInteger:rang.location]];
        NSRange rang1 = {0, 0};
        NSInteger location = 0;
        NSInteger length = 0;
        for (int i = 0;; i++) {
            if (0 == i) {
                location = rang.location + rang.length;
                length = text.length - rang.location - rang.length;
                rang1 = NSMakeRange(location, length);
            } else {
                location = rang1.location + rang1.length;
                length = text.length - rang1.location - rang1.length;
                rang1 = NSMakeRange(location, length);
            }
            rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
            if (rang1.location == NSNotFound && rang1.length == 0) {
                break;
            } else {
                [arrayRanges addObject:[NSNumber numberWithInteger:rang1.location]];
            }
        }
        return arrayRanges;
    }
    return nil;
}

- (BOOL)isLenght {
    
    if (self.length > 0) {
        return YES;
    }
    return NO;
}


- (void)rac_traverseStringWithNextBlock:(void(^)(id x))block completed:(void(^)())completed {
    
    [[self.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
        
        block(x);
    } completed:completed];
}

@end
